package com.lintf.starter.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lintf.starter.central.auth.CentralAuthClient;
import com.lintf.starter.central.auth.CustomToken;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lin tingfeng
 * @ system Mac
 * @ data 2025/6/12 09:42
 * @ package_name com.lintf.starter.auth
 * @ project_name my-diy-project
 * @ desc
 */
@Slf4j
@RequestMapping("/central/auth")
@RestController
public class AuthCentralController {

    @Resource
    private CentralAuthClient centralAuthClient;

    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        return centralAuthClient.getAccessToken();
    }

    @GetMapping("/getAccessTokenInfo")
    public String getAccessTokenInfo() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            CustomToken metadata = centralAuthClient.getTokenMetadata();
            return objectMapper.writeValueAsString(metadata);
        } catch (Exception e) {
            log.error("Error retrieving access token info", e);
            return "Error retrieving access token info";
        }
    }
}
